package cn.wang.controller;

import cn.wang.pojo.Category;
import cn.wang.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    /**
     * 根据父级id查询所有的分类
     * @param pid
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<Category>> queryListByPid(
            @RequestParam(name="pid",required = true,defaultValue = "0") Long pid){ // 设置pid的默认值为0
        //ResponseEntity.status(200).body(categoryService.queryListByPid(pid));
        // 等价于
        return ResponseEntity.ok(categoryService.queryListByPid(pid));
    }

    /**
     * 根据商品id,查询所属分类
     */
    @GetMapping("/bid/{bid}")
    public ResponseEntity<List<Category>> queryByBid(@PathVariable("bid")Long bid){
        return ResponseEntity.ok(categoryService.queryByBid(bid));
    }
    @GetMapping("/cid")
    public ResponseEntity<List<Category>> queryCategorysByCids(@RequestParam("cid")Long... cids){
        return ResponseEntity.ok(categoryService.queryByCids(cids));
    }
}
